import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:yyshort/common/model/record_model.dart';
import 'package:yyshort/common/model/video_model.dart';

import 'index.dart';

class RecordPage extends GetView<RecordController> {
  const RecordPage({Key? key}) : super(key: key);

  // 列表布局
  Widget _buildListView() {
    return ListView.separated(
      padding: const EdgeInsets.all(10),
      itemCount: controller.state.record_modelList.length,
      separatorBuilder: (context, index) =>
          SizedBox.shrink(), //const Divider(height: 0.1),
      itemBuilder: (context, index) {
        return _buildRecordItem(controller.state.record_modelList[index]);
      },
    );
  }

  Widget _buildRecordItem(RecordModel model) {
    // 类型相关配置
    final isRecharge = model.type == 1;
    final typeText = isRecharge ? '充值'.tr : '解锁消费'.tr;
    final typeColor = isRecharge ? Colors.green : Colors.orange;
    final coinText = '${isRecharge ? '+' : ''}${model.coin}';
    final String formattedTime = model.createdAt!;

    // 固定高度：根据内容调整为合适的值（这里示例设置为80）
    return SizedBox(
      height: 80, // 固定高度
      child: InkWell(
        onTap: () {},
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Color(0xFFEEEEEE), width: 0.3),
            ),
          ),
          child: Row(
            children: [
              // 类型图标
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: typeColor.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  isRecharge ? Icons.add_circle : Icons.remove_circle,
                  color: typeColor,
                  size: 24,
                ),
              ),
              const SizedBox(width: 12),

              // 中间信息
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      typeText,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        color: Color.fromARGB(255, 147, 141, 141),
                      ),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      formattedTime,
                      style: const TextStyle(
                        fontSize: 12,
                        color: Color(0xFF999999),
                      ),
                    ),
                  ],
                ),
              ),

              // 金币数量
              Text(
                coinText,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: isRecharge ? Colors.green : Colors.orange,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 在 view.dart 中找到 _buildView 方法并替换为以下实现
  Widget _buildView() {
    return Obx(() {
      return SmartRefresher(
        controller: controller.refreshController,
        enablePullDown: true,
        enablePullUp: true,
        onRefresh: controller.onRefresh,
        onLoading: controller.onLoading,
        // 根据数据是否为空显示不同内容
        child: controller.state.isNeedShowEmpty
            ? _buildEmptyState()
            : _buildListView(),
      );
    });
  }

  // 空状态视图
  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 空数据图片（这里使用网络图片示例，实际项目中建议使用本地资源图片）
          Image.asset(
            'assets/images/empty.png', // 本地未选中状态图片路径
            width: 150,
            height: 150,
            fit: BoxFit.contain,
          ),
          const SizedBox(height: 20),
          // Text(
          //   "暂无消费记录".tr,
          //   style: const TextStyle(fontSize: 16, color: Color(0xFF999999)),
          // ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("消费记录".tr, style: TextStyle(color: Colors.white)),
        // 自定义返回按钮
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.white), // 强制白色
          onPressed: () => Navigator.of(context).pop(), // 返回上一页
        ),
        // backgroundColor: Colors.blue, // 确保与白色文字有对比
      ),
      body: SafeArea(child: _buildView()),
    );
  }
}
